package com.ysd.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ysd.entity.Admin;

//Filter过滤器
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	//过滤的核心方法,所有的请求都会经过该方法
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
			HttpServletRequest req = (HttpServletRequest)request;//将ServletRequest转成HttpServletRequest
			HttpServletResponse res = (HttpServletResponse)response;//将ServletResponse转成HttpServletResponse
			
			String url = req.getRequestURI();
			
			HttpSession session = req.getSession();//创建session对象
			Admin admin = (Admin) session.getAttribute("admin");//session中取数据
			if(url.endsWith("login.html") || url.endsWith(".css")||url.endsWith(".js")||url.endsWith(".jpg")) {
				chain.doFilter(req,res);
			}else if(admin !=null) {//登陆过
				chain.doFilter(req,res);
			}else {//没有登陆过
				res.sendRedirect("login.html");
			}
	
	
	
	
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
